package com.coincalf.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.coincalf.admin.common.CommonUtils;
import com.coincalf.admin.entity.Account;
import com.coincalf.admin.entity.Coin;
import com.coincalf.admin.entity.User;
import com.coincalf.admin.mapper.CoinMapper;
import com.coincalf.admin.service.AccountService;
import com.coincalf.admin.service.CoinService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.coincalf.admin.service.UserService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 币种配置信息 服务实现类
 * </p>
 *
 * @author qiang
 * @since 2018-05-13
 */
@Slf4j
@Service
public class CoinServiceImpl extends ServiceImpl<CoinMapper, Coin> implements CoinService {

    private  static  final  int defaultSize=100;
    @Autowired
    private AccountService accountService;
    @Autowired
    private UserService userService;

    @Override
    @Async
    public void ansyUpdateAccounts(Long coinId) {
        log.info("-----ansyUpdateAccounts----========coinId======"+coinId);
        Integer [] ustyps={CommonUtils.USER_TYPE_0,CommonUtils.USER_TYPE_1};
        List<User>userList=userService.selectList( new EntityWrapper<User>().in("type",ustyps));
        if(userList!=null&&userList.size()>0){

            List<List<User>> rsList = Lists.partition(userList, defaultSize);
            int i=0;
            for (List<User> users : rsList) {
                List<Account> accoutUserIds=new ArrayList<>();
                log.info("-----ansyUpdateAccounts--n:-"+(i++));
                for (User us:users) {
                    Account account=new Account();
                    account.setUserId(us.getId());
                    account.setStatus(CommonUtils.STATUS_1);
                    account.setCoinId(coinId);
                    account.setWithdrawalsAmount(new BigDecimal("0.00000000"));
                    account.setBalanceAmount(new BigDecimal("0.00000000"));
                    account.setFreezeAmount(new BigDecimal("0.00000000"));
                    account.setRechargeAmount(new BigDecimal("0.00000000"));
                    account.setNetValue(new BigDecimal("0.00000000"));
                    account.setLockMargin(new BigDecimal("0.00000000"));
                    account.setFloatProfit(new BigDecimal("0.00000000"));
                    account.setTotalProfit(new BigDecimal("0.00000000"));
                    account.setVersion(0l);
                    accoutUserIds.add(account);
                }
                log.info("-分批插入--"+i+"-----ansyUpdateAccounts----accoutUserIds---======"+accoutUserIds);
                if(accoutUserIds!=null){
                    Boolean rs= accountService.insertBatch(accoutUserIds);
                }
            }



          /*  int n=userList.size()/defaultSize;
            int mt=userList.size()%defaultSize;
            if(mt>0||n<=0){
               n=n+1;
            }
            log.info("-----ansyUpdateAccounts--n:-"+n+"--------m:-"+mt);
            //插入次数
            for (int i=0; i<n;i++){
                int start=i*defaultSize;//0   100 200
                int end=start+defaultSize;//99 199
                List<User>newlist=userList.subList(start,end-1);
                List<Account> accoutUserIds=new ArrayList<>();
                for (User us:newlist) {
                    Account account=new Account();
                    account.setUserId(us.getId());
                    account.setStatus(CommonUtils.STATUS_1);
                    account.setCoinId(coinId);
                    account.setWithdrawalsAmount(new BigDecimal("0.00000000"));
                    account.setBalanceAmount(new BigDecimal("0.00000000"));
                    account.setFreezeAmount(new BigDecimal("0.00000000"));
                    account.setRechargeAmount(new BigDecimal("0.00000000"));
                    account.setNetValue(new BigDecimal("0.00000000"));
                    account.setLockMargin(new BigDecimal("0.00000000"));
                    account.setFloatProfit(new BigDecimal("0.00000000"));
                    account.setTotalProfit(new BigDecimal("0.00000000"));
                    account.setVersion(0l);
                    accoutUserIds.add(account);
                }
                log.info("-分批插入--"+i+"-----ansyUpdateAccounts----accoutUserIds---======"+accoutUserIds);
                if(accoutUserIds!=null){
                    Boolean rs= accountService.insertBatch(accoutUserIds);
                }
            }*/
        }
        log.info("-----ansyUpdateAccounts----result----========coinId======"+coinId);
    }

    public static void main(String[] args) {
        int t=1111/defaultSize;
        int s=1111%defaultSize;
        System.out.println("args = [" + t + "]"+"--s:"+s);
    }
}
